
class Circuit():
	def __init__(self, path, graph, z_vertice=None):
		self.path = path
		self._calculate_weight(graph)
		self._create_binary_vector(graph)
		self.z = z_vertice
		#print path
		#print graph.edges()
		#print self.binary_vector

	def _calculate_weight(self, graph):
		# calculate weight here or find a better shortest-path algorithm...
		weight = 0.0
		node_start = self.path[0]
		for node_end in self.path[1:]:
			weight = weight + graph[node_start][node_end]['weight']
			node_start = node_end
		weight = weight + graph[node_end][self.path[0]]['weight']
		self.weight = weight

	def _create_binary_vector(self, graph):
		edges = graph.edges()
		edges.sort()			# edges() seem to produce sorted edges, but it isn't explicitely stated in doc
		self.binary_vector = [0] * len(edges)
		node_start = self.path[0]
		for node_end in self.path[1:]:
			edge = (node_start, node_end)
			pos = edges.index(edge)
			self.binary_vector[pos] = 1
			node_start = node_end
		edge = (self.path[len(self.path) - 1], self.path[0])
		pos = edges.index(edge)
		self.binary_vector[pos] = 1

	def _create_edge_list(self):
		node_start = self.path[0]
		node_list = []
		for node_end in self.path[1:]:
			edge = (node_start, node_end)
			node_list.append(edge)
			node_start = node_end
			node_list.append((node_end, self.path[0]))
		return node_list

	def __str__(self):
		if self.z:
			prefix = "(z:%s)" % str(self.z)
		else:
			prefix = ""
		return "(" + prefix + self.path.__str__() + ": " + str(self.weight) + ")"

	def __repr__(self):
		return self.__str__()

	def __eq__(self, other):
		return self.binary_vector == other.binary_vector

